{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/env python\n",
    "\n",
    "'''\n",
    "\n",
    "Label标签 & Button按钮\n",
    "\n",
    "'''\n",
    "\n",
    "import tkinter as tk\n",
    "\n",
    "root = tk.Tk()\n",
    "root.title('Label标签 & Button按钮')\n",
    "root.geometry('1000x500')\n",
    "\n",
    "xianshi = tk.StringVar()\n",
    "if_hit = True\n",
    "\n",
    "label = tk.Label(root, textvariable=xianshi, bg='green',font=('Arial',14),width=30,height=10)\n",
    "# Label 中的 width、height 单位是一个字符宽度\n",
    "label.pack()\n",
    "\n",
    "def hit_me():\n",
    "    global if_hit\n",
    "    if if_hit == True:\n",
    "        if_hit = False\n",
    "        xianshi.set('点了一下，再点一下恢复')\n",
    "    else:\n",
    "        xianshi.set('还没点击按钮！')\n",
    "        if_hit = True    \n",
    "        \n",
    "button = tk.Button(root, text='点我一下！', command=hit_me, width=20, height=2)\n",
    "button.pack()\n",
    "    \n",
    "tk.mainloop()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/env python\n",
    "\n",
    "'''\n",
    "\n",
    "Entry输入 & Text文本框\n",
    "\n",
    "'''\n",
    "\n",
    "import tkinter as tk\n",
    "\n",
    "root = tk.Tk()\n",
    "root.title('Entry输入 & Text文本框')\n",
    "root.geometry('700x700')\n",
    "\n",
    "entry = tk.Entry(root, show=None)\n",
    "# show 可选择 None\\'1'(输入不管是什么都显示为数字1)\\'*' 等等\n",
    "entry.pack()\n",
    "\n",
    "# 按下button按钮，entry输入内容插入到text中的光标处\n",
    "def insert_point():\n",
    "    tempvar = entry.get()\n",
    "    text.insert('insert', tempvar)\n",
    "    \n",
    "button = tk.Button(root, text='插入到光标处', width=20, height=10, command=insert_point)\n",
    "button.pack()\n",
    "\n",
    "def insert_end():\n",
    "    tempvar1 = entry.get()\n",
    "    text.insert('end', tempvar1)\n",
    "button1 = tk.Button(root, text='插入到字符串末尾处', width=20, height=10, command=insert_end)\n",
    "button1.pack()\n",
    "\n",
    "def insert_somewhere():\n",
    "    tempvar1 = entry.get()\n",
    "    text.insert(1.1, tempvar1)\n",
    "button1 = tk.Button(root, text='插入到第一行第一位处', width=20, height=10, command=insert_somewhere)\n",
    "button1.pack()\n",
    "\n",
    "text = tk.Text(root, height=2)\n",
    "text.pack()\n",
    "\n",
    "tk.mainloop()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/env python\n",
    "\n",
    "'''\n",
    "\n",
    "Listbox列表组件\n",
    "\n",
    "'''\n",
    "\n",
    "import tkinter as tk\n",
    "\n",
    "root = tk.Tk()\n",
    "root.title('Listbox 列表组件')\n",
    "root.geometry('1000x1000')\n",
    "\n",
    "selectVar = tk.StringVar()\n",
    "\n",
    "label = tk.Label(root, textvariable=selectVar, bg='green', width=20,height=10)\n",
    "label.pack()\n",
    "\n",
    "def insert_up():\n",
    "    value=listbox.get(listbox.curselection())\n",
    "    selectVar.set(value)\n",
    "    \n",
    "    \n",
    "button = tk.Button(root, text='输出选择项', width=20, height=10, command=insert_up)\n",
    "button.pack()\n",
    "\n",
    "listvar = tk.StringVar()\n",
    "listvar.set(('nihao',11,'上海'))\n",
    "\n",
    "listbox = tk.Listbox(root, listvariable=listvar)\n",
    "listbox.insert(1,'浙江')\n",
    "listbox.pack()\n",
    "\n",
    "tk.mainloop()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/env python\n",
    "\n",
    "'''\n",
    "\n",
    "Radiobutton 选择按钮\n",
    "\n",
    "'''\n",
    "\n",
    "import tkinter as tk\n",
    "\n",
    "root = tk.Tk()\n",
    "root.title('Radiobutton 选择按钮')\n",
    "root.geometry('500x500')\n",
    "\n",
    "selectOption = tk.StringVar()\n",
    "#radioOption = tk.StringVar()\n",
    "\n",
    "label = tk.Label(root, text='', bg='yellow', width=200, font=(20))\n",
    "label.pack()\n",
    "\n",
    "def printSelectOpt():\n",
    "    label.config(text='你选择了选项'+selectOption.get())\n",
    "\n",
    "\n",
    "radioButton1 = tk.Radiobutton(root, text='选项 A', variable=selectOption, value='A', command=printSelectOpt)\n",
    "radioButton1.pack()\n",
    "radioButton2 = tk.Radiobutton(root, text='选项 B', variable=selectOption, value='B', command=printSelectOpt)\n",
    "radioButton2.pack()\n",
    "radioButton3 = tk.Radiobutton(root, text='选项 C', variable=selectOption, value='C', command=printSelectOpt)\n",
    "radioButton3.pack()\n",
    "tk.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/env python\n",
    "\n",
    "'''\n",
    "\n",
    "scale拉条\n",
    "\n",
    "'''\n",
    "\n",
    "import tkinter as tk\n",
    "\n",
    "root = tk.Tk()\n",
    "root.title('Scale拉条')\n",
    "root.geometry('500x500')\n",
    "\n",
    "label = tk.Label(root, text='', bg='green', width=30, height=5)\n",
    "label.pack()\n",
    "\n",
    "def print_selection(v):\n",
    "    label.config(text='你选择了' + v)\n",
    "\n",
    "scale = tk.Scale(root, label='横向拉条', from_=0, to=10, orient=tk.HORIZONTAL, length=300, showvalue=1, \n",
    "                 tickinterval=2, resolution=0.01, command=print_selection)\n",
    "'''\n",
    "label 进度条名字 from_ 进度条起点，to 进度条终，orient 选择是水平还是竖直显示进度条 length 进度条屏幕上显示长度，单位是一个像素，\n",
    "showvalue 控制是否在进度条上显示选择数值，0-不显示,非0-显示，tickinterval 分割单位，resolution 精确度\n",
    "'''\n",
    "scale.pack()\n",
    "\n",
    "root.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/env python\n",
    "\n",
    "'''\n",
    "\n",
    "checkbutton 勾选项\n",
    "\n",
    "'''\n",
    "\n",
    "import tkinter as tk\n",
    "\n",
    "root = tk.Tk()\n",
    "root.title('Checkbutton 勾选项')\n",
    "root.geometry('500x500')\n",
    "\n",
    "label = tk.Label(root, text='请选择您会的编程语言', bg='green', width=30, height=5)\n",
    "label.pack()\n",
    "\n",
    "'''\n",
    "var0 = tk.IntVar()\n",
    "var1 = tk.IntVar()\n",
    "上两行一定要放在 print_selection()函数之后\n",
    "'''\n",
    "def print_selection():\n",
    "    if (var0.get() == 0) & (var1.get() == 0):\n",
    "        label.config(text='我什么都不会！')\n",
    "    elif (var0.get() == 1) & (var1.get() == 0):\n",
    "        label.config(text='我只会 Python')\n",
    "    elif (var0.get() == 0) & (var1.get() == 1):\n",
    "        label.config(text='我只会 C++')\n",
    "    elif (var0.get() == 1) & (var1.get() == 1):\n",
    "        label.config(text='我两个都会！')\n",
    "        \n",
    "var0 = tk.IntVar()\n",
    "var1 = tk.IntVar()\n",
    "\n",
    "checkbutton0 = tk.Checkbutton(root, text='Python', variable=var0, onvalue=1, offvalue=0, command=print_selection)\n",
    "'''\n",
    "若选定 checkbutton0(即 Python 选项)，则 var0 被赋值为 onvalue 值（1），反之被赋值为 offvalue 值（0）\n",
    "'''\n",
    "checkbutton0.pack()\n",
    "\n",
    "checkbutton1 = tk.Checkbutton(root, text='C++', variable=var1, onvalue=1, offvalue=0, command=print_selection)\n",
    "checkbutton1.pack()\n",
    "\n",
    "root.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/env python\n",
    "'''\n",
    "\n",
    "canvas 画布\n",
    "\n",
    "'''\n",
    "import tkinter as tk\n",
    "\n",
    "root = tk.Tk()\n",
    "root.title('Canvas 画布')\n",
    "root.geometry('500x500')\n",
    "\n",
    "canvas = tk.Canvas(root, bg='green', width=300, height=200)\n",
    "#photo_file=tk.PhotoImage(file='/home/xcq/桌面/tenor.gif')\n",
    "#photo = canvas.create_image(0,0,anchor='nw',image = photo_file)\n",
    "canvas.pack()\n",
    "\n",
    "button = tk.Button(root, text='向下移动图形').pack()\n",
    "\n",
    "root.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/env python\n",
    "\n",
    "'''\n",
    "\n",
    "menubar 菜单\n",
    "\n",
    "'''\n",
    "\n",
    "import tkinter as tk\n",
    "\n",
    "root = tk.Tk()\n",
    "root.title('menubar 菜单')\n",
    "root.geometry('500x500')\n",
    "\n",
    "label = tk.Label(root, text='',bg='green', width=30, height=5)\n",
    "label.pack()\n",
    "\n",
    "count = 0\n",
    "def do_job():\n",
    "    global count\n",
    "    label.config(text='执行了'+str(count))\n",
    "    count+=1\n",
    "\n",
    "menubar = tk.Menu(root)\n",
    "filemenu = tk.Menu(menubar, tearoff=False)\n",
    "filemenu.add_command(label='新建', command=do_job)\n",
    "filemenu.add_command(label='保存', command=do_job)\n",
    "filemenu.add_separator()\n",
    "filemenu.add_command(label='退出', command=root.quit)\n",
    "menubar.add_cascade(label='菜单', menu=filemenu)\n",
    "\n",
    "editmenu = tk.Menu(menubar, tearoff=False)\n",
    "editmenu.add_command(label='撤回', command=do_job)\n",
    "menubar.add_cascade(label='编辑', menu=editmenu)\n",
    "\n",
    "submenu = tk.Menu(editmenu, tearoff=False)\n",
    "editmenu.add_cascade(label='编辑', menu=submenu,underline=0)\n",
    "submenu.add_command(label='子目录', command=do_job)\n",
    "\n",
    "root.config(menu = menubar)\n",
    "\n",
    "root.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/env python\n",
    "\n",
    "'''\n",
    "\n",
    " Frame 框架\n",
    "\n",
    "'''\n",
    "\n",
    "import tkinter as tk\n",
    "\n",
    "root = tk.Tk()\n",
    "root.title('Frame 框架')\n",
    "root.geometry('500x500')\n",
    "\n",
    "tk.Label(root, text='最上方', bg='white').pack()\n",
    "\n",
    "frame = tk.Frame(root)\n",
    "frame.pack()\n",
    "frame_left = tk.Frame(frame)\n",
    "frame_right = tk.Frame(frame)\n",
    "frame_left.pack(side='left')\n",
    "frame_right.pack(side='right')\n",
    "\n",
    "tk.Label(frame_left, text='左侧框架' , font='20', bg='yellow').pack()\n",
    "tk.Label(frame_left, text='左侧框架第二行',font='20', bg='green').pack()\n",
    "tk.Label(frame_right, text='右侧框架', font='20', bg='red').pack()\n",
    "\n",
    "root.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception in Tkinter callback\n",
      "Traceback (most recent call last):\n",
      "  File \"/usr/lib/python3.6/tkinter/__init__.py\", line 1705, in __call__\n",
      "    return self.func(*args)\n",
      "  File \"<ipython-input-18-9429ca00d64f>\", line 16, in hit_me\n",
      "    messagebox.showinfo(title='nihao', message='woyehao')\n",
      "NameError: name 'messagebox' is not defined\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/env python\n",
    "\n",
    "'''\n",
    "\n",
    "messagebox 弹窗 \n",
    "\n",
    "'''\n",
    "import tkinter as tk\n",
    "\n",
    "root = tk.Tk()\n",
    "root.title('messagebox 弹窗 ')\n",
    "root.geometry('500x500')\n",
    "\n",
    "def hit_me():\n",
    "    tk.messagebox.showinfo(title='nihao', message='woyehao')\n",
    "\n",
    "tk.Button(root, text='点击我', command=hit_me).pack()\n",
    "\n",
    "root.mainloop()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
